/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.daistatistic.charts;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import soccerscope.daistatistic.StatisticDialog;
import soccerscope.daistatistic.tools.TextWriter;

/**
 * this class represents a frame which can show and save text lines
 * 
 */
public class InfoFrame extends Frame implements WindowListener, ActionListener {

	private static final long serialVersionUID = 3832625079791137331L;

	private MenuItem saveChart; // allocate the save menu item

	private String fileName = "infoFrameOutput"; // the file name

	// init size and location
	private int sizeX = 400;

	private int sizeY = 540;

	private int locationX = 300;

	private int locationY = 300;

	String[] lines = new String[0];

	/**
	 * constructor init the frame and add the menu bar
	 */
	public InfoFrame() {
		setVisible(false);

		setSize(this.sizeX, this.sizeY);
		setLocation(this.locationX, this.locationY);

		addWindowListener(this);

		MenuBar menuBar = new MenuBar();
		this.saveChart = new MenuItem("Save Chart");
		this.saveChart.addActionListener(this);
		Menu fileMenu = new Menu("File");
		fileMenu.add(this.saveChart);
		menuBar.add(fileMenu);
		this.setMenuBar(menuBar);

		this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
		this.setTitle("Text statistic output");

		doLayout();
		setVisible(true);
	}

	/**
	 * setting the shown image
	 * 
	 * @param BufferedImage
	 *            image
	 */
	public void setInfoLines(String[] lines) {
		this.lines = lines;
		repaint();
	}

	/**
	 * setting the file name
	 * 
	 * @param String
	 *            name
	 */
	public void setFileName(String name) {
		this.fileName = name;
	}

	/**
	 * repaint the context
	 */
	@Override
	public void repaint() {
		paint(this.getGraphics());
	}

	@Override
	public void paint(Graphics g) {
		g.clearRect(0, 0, this.sizeX, this.sizeY);
		int y = 45;
		for (int i = 0; i < this.lines.length; i++) {
			g.drawString(this.lines[i], 20, y);
			y = y + 15;
		}
	}

	/**
	 * action handler fpr the menu bar
	 * 
	 * @param ActionEvent
	 *            arg0, the sender
	 */
	public void actionPerformed(ActionEvent arg0) {
		Object o = arg0.getSource();

		if (o == this.saveChart) {
			saveToTXT();
			return;
		}

	}

	/**
	 * save the shown text to a .txt file
	 */
	private void saveToTXT() {
		TextWriter.save(this.lines, this.fileName);
	}

	public void windowOpened(WindowEvent arg0) {

	}

	public void windowClosing(WindowEvent arg0) {
		StatisticDialog.infoFrameClosed();
		dispose();

	}

	public void windowClosed(WindowEvent arg0) {

	}

	public void windowIconified(WindowEvent arg0) {

	}

	public void windowDeiconified(WindowEvent arg0) {

	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowDeactivated(WindowEvent arg0) {
	}
}

